#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/wait.h>

int Sum(int start, int end)
{
    // sleep(100);
    int sum = 0;
    for(int i = start; i <= end; i++)
    {
        sum /= 0; // core
        sum += i;
    }
    return sum;
}

int main()
{
    // int total = Sum(0, 100);
    // std::cout << "total: " << total << std::endl;
    
    // 会生成core文件，调试时直接加载可以直接找到错误
    // 事后调试
    pid_t id = fork();
    if(id == 0)
    {
        sleep(1);
        // child
        Sum(0, 100);
        exit(0);
    }
    // father
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if(rid == id)
    {
        printf("exit code: %d, exit sig: %d, core dump: %d\n", (status>>8)&0xFF, status&0x7F, (status>>7)&0x1);
    }
    return 0;
}